package com.zzyl.nursing.domain.pojo;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * 08:00 - 18:00 每半小时一个时间区间的枚举类
 */
public enum TimeSlotEnum {
    // 08:00 - 08:30
    SLOT_0800_0830(LocalTime.of(8, 0), LocalTime.of(8, 30)),
    // 08:30 - 09:00
    SLOT_0830_0900(LocalTime.of(8, 30), LocalTime.of(9, 0)),
    // 09:00 - 09:30
    SLOT_0900_0930(LocalTime.of(9, 0), LocalTime.of(9, 30)),
    // 09:30 - 10:00
    SLOT_0930_1000(LocalTime.of(9, 30), LocalTime.of(10, 0)),
    // 10:00 - 10:30
    SLOT_1000_1030(LocalTime.of(10, 0), LocalTime.of(10, 30)),
    // 10:30 - 11:00
    SLOT_1030_1100(LocalTime.of(10, 30), LocalTime.of(11, 0)),
    // 11:00 - 11:30
    SLOT_1100_1130(LocalTime.of(11, 0), LocalTime.of(11, 30)),
    // 11:30 - 12:00
    SLOT_1130_1200(LocalTime.of(11, 30), LocalTime.of(12, 0)),
    // 12:00 - 12:30
    SLOT_1200_1230(LocalTime.of(12, 0), LocalTime.of(12, 30)),
    // 12:30 - 13:00
    SLOT_1230_1300(LocalTime.of(12, 30), LocalTime.of(13, 0)),
    // 13:00 - 13:30
    SLOT_1300_1330(LocalTime.of(13, 0), LocalTime.of(13, 30)),
    // 13:30 - 14:00
    SLOT_1330_1400(LocalTime.of(13, 30), LocalTime.of(14, 0)),
    // 14:00 - 14:30
    SLOT_1400_1430(LocalTime.of(14, 0), LocalTime.of(14, 30)),
    // 14:30 - 15:00
    SLOT_1430_1500(LocalTime.of(14, 30), LocalTime.of(15, 0)),
    // 15:00 - 15:30
    SLOT_1500_1530(LocalTime.of(15, 0), LocalTime.of(15, 30)),
    // 15:30 - 16:00
    SLOT_1530_1600(LocalTime.of(15, 30), LocalTime.of(16, 0)),
    // 16:00 - 16:30
    SLOT_1600_1630(LocalTime.of(16, 0), LocalTime.of(16, 30)),
    // 16:30 - 17:00
    SLOT_1630_1700(LocalTime.of(16, 30), LocalTime.of(17, 0)),
    // 17:00 - 17:30
    SLOT_1700_1730(LocalTime.of(17, 0), LocalTime.of(17, 30)),
    // 17:30 - 18:00
    SLOT_1730_1800(LocalTime.of(17, 30), LocalTime.of(18, 0));

    private final LocalTime startTime;
    private final LocalTime endTime;

    /**
     * 构造函数，初始化时间区间的起始和结束时间
     * @param startTime 时间区间的起始时间
     * @param endTime 时间区间的结束时间
     */
    TimeSlotEnum(LocalTime startTime, LocalTime endTime) {
        this.startTime = startTime;
        this.endTime = endTime;
    }

    /**
     * 获取时间区间的起始时间
     * @return 起始时间
     */
    public LocalTime getStartTime() {
        return startTime;
    }

    /**
     * 获取时间区间的结束时间
     * @return 结束时间
     */
    public LocalTime getEndTime() {
        return endTime;
    }

    /**
     * 将时间区间转换为易读的字符串格式
     * @return 格式为 "HH:mm - HH:mm" 的字符串
     */
    @Override
    public String toString() {
        return startTime.format(java.time.format.DateTimeFormatter.ofPattern("HH:mm")) + 
               " - " + 
               endTime.format(java.time.format.DateTimeFormatter.ofPattern("HH:mm"));
    }


    /**
     * 根据传入的 LocalDateTime 时间判断其处于哪个时间阶段，返回对应阶段开始时间的字符串
     * @param localDateTime 传入的 LocalDateTime 时间
     * @return 格式为 "yyyy-MM-dd HH:mm:ss" 的字符串，如果不在 08:00 - 18:00 范围内返回 null
     */
    public static String getTimeStage(LocalDateTime localDateTime) {
        LocalTime localTime = localDateTime.toLocalTime();
        for (TimeSlotEnum slot : TimeSlotEnum.values()) {
            if (!localTime.isBefore(slot.startTime) && localTime.isBefore(slot.endTime)) {
                LocalDateTime stageStartTime = localDateTime.with(slot.startTime);
                return stageStartTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            }
        }
        return null;
    }
}